package light.head.controller.www;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.config.ConfigParm;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.constant.Url;
import light.head.dao.site.EnergyConservationDAO;
import light.head.dao.site.ParkAnnouncementDAO;
import light.head.dao.site.ParkCommodityDAO;
import light.head.dao.site.ParkOrganDAO;
import light.head.dao.site.ParkRecruitDAO;
import light.head.dao.site.PolicySupportAnnexDAO;
import light.head.dao.site.PolicySupportDAO;
import light.head.dao.site.RecruitInfoDAO;
import light.head.dao.site.SupplyAnnexDAO;
import light.head.dao.site.SupplyInfoDAO;
import light.head.dao.site.WorkGuideAnnexDAO;
import light.head.dao.site.WorkGuideDAO;
import light.head.dao.site.WorkGuideDetailDAO;
import light.head.dao.sys.OrganDAO;
import light.head.framework.base.BaseController;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.EnergyConservation;
import light.head.model.ParkAnnouncement;
import light.head.model.ParkCommodity;
import light.head.model.ParkOrgan;
import light.head.model.ParkRecruit;
import light.head.model.PolicySupport;
import light.head.model.PolicySupportAnnex;
import light.head.model.RecruitInfo;
import light.head.model.SupplyAnnex;
import light.head.model.SupplyInfo;
import light.head.model.WorkGuide;
import light.head.model.WorkGuideAnnex;
import light.head.model.WorkGuideDetail;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author janhang
 * @2010-12-6 下午04:19:08 首页
 */
@Controller
public class IndexController extends BaseController {
	private ParkAnnouncementDAO paDAO;
	private EnergyConservationDAO ecDAO;
	private PolicySupportDAO psDAO;
	private ParkOrganDAO poDAO;
	private ParkCommodityDAO pcDAO;
	private WorkGuideDetailDAO wgdDAO;
	private WorkGuideAnnexDAO wgaDAO;
	private WorkGuideDAO wgDAO;
	private SupplyInfoDAO siDAO;
	private SupplyAnnexDAO saDAO;
	private OrganDAO oDAO;
	private ParkRecruitDAO prDAO;
	private RecruitInfoDAO riDAO;
	private PolicySupportAnnexDAO psaDAO;
	
	@RequestMapping(value = Url.C_INDEX)
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<ParkAnnouncement> paList = paDAO.getIndexList();
		List<EnergyConservation> ecList = ecDAO.getIndexList();
		List<PolicySupport> pslist1 = psDAO.getIndexList(Byte.parseByte(Sys.S_ONE));
		List<PolicySupport> pslist2 = psDAO.getIndexList(Byte.parseByte(Sys.S_TWO));
		List<PolicySupport> pslist3 = psDAO.getIndexList(Byte.parseByte(Sys.S_THREE));
		List<WorkGuide> wgoList = wgDAO.getIndexList();
		List<ParkOrgan> poList = poDAO.getIndexList();
		List<ParkCommodity> pcList = pcDAO.getIndexList();
		List<ParkRecruit> prList = prDAO.getIndexList();
		// 园区公告
		request.setAttribute(Parm.PALIST, paList);
		// 节能减排
		request.setAttribute(Parm.ECLIST, ecList);
		// 政策支持
		request.setAttribute(Parm.PSLIST1, pslist1);
		request.setAttribute(Parm.PSLIST2, pslist2);
		request.setAttribute(Parm.PSLIST3, pslist3);
		// 办事指南
		request.setAttribute(Parm.WGOLIST, wgoList);
		// 园区企业
		request.setAttribute(Parm.POLIST, poList);
		//招聘信息
		request.setAttribute(Parm.PRLIST, prList);
		// 园区产品
		request.setAttribute(Parm.PCLIST, pcList);
		return new ModelAndView(Url.INDEX);
	}

	/**
	 * 列表页跳转
	 */
	@RequestMapping(value = Url.INITLIST)
	public ModelAndView initList(String type, HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setAttribute(Parm.TYPE, type);
		return new ModelAndView(Url.INIT_LIST);
	}

	/**
	 * 首页所有列表
	 */
	@RequestMapping(Url.QUERY)
	public ModelAndView queryList(String ctype, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME,Parm.TITLE,Parm.TIME1, Parm.TIME2, Parm.DELETED, Parm.TYPE, Parm.STATUS ,Parm.POST);
		map.put(Parm.ET, DateUtils.getDateStr(new Date(), "yyyy-MM-ddd"));
		map.put(Parm.DELETED,Sys.S_ZERO);
		PageBean pb = createPageBean(request);
		String url = null;
		if (ctype != null) {
			//园区公告
			if (ctype.equals("pa")) {
				paDAO.findByMap(map, pb);
				url = Url.PA_LIST;
			//节能减排
			} else if (ctype.equals("ec")) {
				ecDAO.findByMap(map, pb);
				url = Url.EC_LIST;
			//优惠政策
			} else if (ctype.equals("fa")) {
				psDAO.findByMap(map, pb);
				url = Url.FA_LIST;
			//招商信息
			} else if (ctype.equals("me")) {
				psDAO.findByMap(map, pb);
				url = Url.ME_LIST;
			//申报项目
			} else if (ctype.equals("de")) {
				psDAO.findByMap(map, pb);
				url = Url.DE_LIST;
			//园区企业
			} else if (ctype.equals("po")) {
				poDAO.findByMap(map, pb);
				List<SupplyInfo> siList = siDAO.getList();
				url = Url.PO_LIST;
				request.setAttribute(Parm.SILIST, siList);
			//园区产品
			} else if (ctype.equals("pc")) {
				pcDAO.findByMap(map, pb);
				url = Url.PC_LIST;
			//办事指南(机构)
			} else if (ctype.equals("wgo")) {
				String name = request.getParameter(Parm.NAME);
				List<WorkGuide> wgolist = null;
				if(isNotNull(name)){
					wgolist = wgDAO.getByName(name);
				}else{
					wgolist = wgDAO.getAllList();
				}
				url = Url.WGO_LIST;
				request.setAttribute(Parm.WGOLIST, wgolist);
			//供应信息
			} else if (ctype.equals("si")) {
				siDAO.findByMap(map, pb);
				List<SupplyInfo> siList = siDAO.getBidList();
				url = Url.SI_LIST;
				request.setAttribute(Parm.SILIST, siList);
			//园区地图
			} else if (ctype.equals("map")){
				String search = request.getParameter(Parm.SEARCH);
				List<Object> list = null;
				if(isNotNull(search)){
					 list = oDAO.getRootOrganByName(search);
				}else{
					 list = oDAO.getAllRootOrgan();
				}
				if(list != null && list.size() > 0){
					StringBuffer str = new StringBuffer("[");
					Object[] obj = null;
					for (Object o : list) {
						obj = (Object[]) o;
						str.append("{\"info\":");
						str.append("[");
						
						str.append("\"<a href='" + ConfigParm.getContextPath() + "organindex.html?code="+ obj[1] + "'>" + obj[0].toString() + "</a>\",");
						str.append("\"电话:" + ((null == obj[3]) ? "" : obj[3].toString()) + "地址:" + ((null == obj[2]) ? "" : obj[2].toString()) + "\"");
						str.append("],");
						
						str.append("\"lng\":\"" + ((null == obj[4] || obj[4].toString().equals("")) ? "non" : obj[4]) + "\",");
						str.append("\"lat\":\"" + ((null == obj[5] || obj[5].toString().equals("")) ? "non" : obj[5]) + "\"},");
					}
					str.deleteCharAt(str.length() - 1).append("]");
					request.setAttribute(Parm.ORGAN, str.toString());
				}
				request.setAttribute(Parm.SEARCH, search);
				url = Url.PARK_MAP;
			//园区招聘
			} else if(ctype.equals("pr")){
				prDAO.findByMap(map, pb);
				request.setAttribute(Parm.POST, map.get(Parm.POST));
				url = Url.PR_LIST;
			} 
		}
		request.setAttribute(Parm.TITLE, map.get(Parm.TITLE));
		request.setAttribute(Parm.NAME, map.get(Parm.NAME));
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(url);
	}

	/**
	 * 详情页跳转
	 */
	@RequestMapping(value = Url.INITDETAIL)
	public ModelAndView initDetail(String type, Integer code, HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setAttribute(Parm.TYPE, type);
		request.setAttribute(Parm.CODE, code);
		return new ModelAndView(Url.INIT_DETAIL);
	}

	/**
	 * 首页所有详情页面
	 */
	@RequestMapping(Url.DETAIL)
	public ModelAndView detail(Integer code, String ctype, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String url = null;
		if (ctype != null && code != null) {
			//园区公告
			if (ctype.equals("pa")) {
				ParkAnnouncement info = paDAO.get(code);
				url = Url.PA_DETAIL;
				request.setAttribute(Parm.INFO, info);
			//节能减排
			} else if (ctype.equals("ec")) {
				EnergyConservation info = ecDAO.get(code);
				url = Url.EC_DETAIL;
				request.setAttribute(Parm.INFO, info);
			//优惠政策
			} else if (ctype.equals("fa")) {
				PolicySupport info = psDAO.getDetail(code, Sys.S_ONE);
				List<PolicySupportAnnex> list = psaDAO.getAllList(code);
				if(list != null && list.size() > 0 ){
					request.setAttribute(Parm.LIST_2, list);
				}
				url = Url.PS_DETAIL;
				request.setAttribute(Parm.INFO, info);
			//招商信息
			} else if (ctype.equals("me")) {
				PolicySupport info = psDAO.getDetail(code, Sys.S_TWO);
				List<PolicySupportAnnex> list = psaDAO.getAllList(code);
				if(list != null && list.size() > 0 ){
					request.setAttribute(Parm.LIST_2, list);
				}
				url = Url.PS_DETAIL;
				request.setAttribute(Parm.INFO, info);
			//申报项目
			} else if (ctype.equals("de")) {
				PolicySupport info = psDAO.getDetail(code, Sys.S_THREE);
				List<PolicySupportAnnex> list = psaDAO.getAllList(code);
				if(list != null && list.size() > 0 ){
					request.setAttribute(Parm.LIST_2, list);
				}
				url = Url.PS_DETAIL;
				request.setAttribute(Parm.INFO, info);
			//办事指南(机构)
			} else if (ctype.equals("wgo")) {
				WorkGuide info = wgDAO.get(code);
				List<WorkGuide> wgpList = wgDAO.getByPc(code);
				for(int i = 0; i< wgpList.size(); i++){
					wgpList.get(i).setSonList(wgDAO.getByPc(wgpList.get(i).getCode()));
				}
				url = Url.WGO_DETAIL;
				request.setAttribute(Parm.INFO, info);
				request.setAttribute(Parm.WGPLIST, wgpList);
			//办事指南(详情)
			} else if (ctype.equals("wgd")) {
				WorkGuide wg = wgDAO.get(code);
				WorkGuideDetail info = wgdDAO.getDetail(code);
				if(info != null){
					List<WorkGuideAnnex> list = wgaDAO.getAnnexList(info.getCode());
					if(list != null && list.size() > 0){
						info.setList(list);
					}
				}
				url = Url.WGD_DETAIL;
				request.setAttribute(Parm.NAME, wg.getPn());
				request.setAttribute(Parm.INFO, info);
			//供应信息
			} else if (ctype.equals("si")) {
				SupplyInfo info = siDAO.get(code);
				info.setBrowseNum(info.getBrowseNum() + 1);
				siDAO.upd(info);
				SupplyAnnex sia = saDAO.getCover(code);
				List<SupplyInfo> suList = siDAO.getBidList();
				url = Url.SI_DETAIL;
				request.setAttribute(Parm.SILIST, suList);
				request.setAttribute(Parm.INFO, info);
				request.setAttribute(Parm.SIA, sia);
			//园区招聘
			}else if (ctype.equals("pr")) {
				RecruitInfo info = riDAO.get(code);
				url = Url.PR_DETAIL;
				request.setAttribute(Parm.INFO,info);
			//联系我们
			}else if(ctype.equals("cus")){
				url = Url.CONTACT_US;
			//关于我们
			}else if(ctype.equals("aus")){
				url = Url.ABOUT_US;
			//网站地图
			}else if(ctype.equals("sm")){
				url = Url.SITE_MAP;
			//使用帮助
			}else if(ctype.equals("help")){
				url = Url.USE_HELP;
			}
		}
		return new ModelAndView(url);
	}
	
	@RequestMapping(value = Url.MAPFULLSCREEN)
	public ModelAndView mapFullScreen(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<Object> list = oDAO.getAllRootOrgan();
		if(list != null && list.size() > 0){
			StringBuffer str = new StringBuffer("[");
			Object[] obj = null;
			for (Object o : list) {
				obj = (Object[]) o;
				str.append("{\"info\":");
				str.append("[");
				
				str.append("\"<a href='" + ConfigParm.getContextPath() + "organindex.html?code="+ obj[1] + "'>" + obj[0].toString() + "</a>\",");
				str.append("\"电话:" + ((null == obj[3]) ? "" : obj[3].toString()) + "地址:" + ((null == obj[2]) ? "" : obj[2].toString()) + "\"");
				str.append("],");
				
				str.append("\"lng\":\"" + ((null == obj[4] || obj[4].toString().equals("")) ? "non" : obj[4]) + "\",");
				str.append("\"lat\":\"" + ((null == obj[5] || obj[5].toString().equals("")) ? "non" : obj[5]) + "\"},");
			}
			str.deleteCharAt(str.length() - 1).append("]");
			request.setAttribute(Parm.ORGAN, str.toString());
		}
		return new ModelAndView(Url.MAP_FULLSCREEN);
	}
	
	@Autowired
	public void setPaDAO(ParkAnnouncementDAO paDAO) {
		this.paDAO = paDAO;
	}

	@Autowired
	public void setEcDAO(EnergyConservationDAO ecDAO) {
		this.ecDAO = ecDAO;
	}

	@Autowired
	public void setPsDAO(PolicySupportDAO psDAO) {
		this.psDAO = psDAO;
	}

	@Autowired
	public void setPoDAO(ParkOrganDAO poDAO) {
		this.poDAO = poDAO;
	}

	@Autowired
	public void setPcDAO(ParkCommodityDAO pcDAO) {
		this.pcDAO = pcDAO;
	}


	@Autowired
	public void setWgdDAO(WorkGuideDetailDAO wgdDAO) {
		this.wgdDAO = wgdDAO;
	}

	@Autowired
	public void setWgaDAO(WorkGuideAnnexDAO wgaDAO) {
		this.wgaDAO = wgaDAO;
	}

	@Autowired
	public void setSiDAO(SupplyInfoDAO siDAO) {
		this.siDAO = siDAO;
	}

	@Autowired
	public void setSaDAO(SupplyAnnexDAO saDAO) {
		this.saDAO = saDAO;
	}
	@Autowired
	public void setoDAO(OrganDAO oDAO) {
		this.oDAO = oDAO;
	}
	@Autowired
	public void setPrDAO(ParkRecruitDAO prDAO) {
		this.prDAO = prDAO;
	}
	@Autowired
	public void setRiDAO(RecruitInfoDAO riDAO) {
		this.riDAO = riDAO;
	}
	@Autowired
	public void setWgDAO(WorkGuideDAO wgDAO) {
		this.wgDAO = wgDAO;
	}
	@Autowired
	public void setPsaDAO(PolicySupportAnnexDAO psaDAO) {
		this.psaDAO = psaDAO;
	}
}
